// JavaScript Document
$(document).ready(function(){
	/*列表控件*/
	$('#sendemail_last_tt').datagrid({
		title: '邮件群发队列'
		, fit: true
		, url: 'sendemail-last.php?method=GetData'
		, pagination: true /*底部分页栏*/
		, singleSelect: true
		, pageList: [100, 200, 500]
		, loadMsg: '正在加载数据，请稍等 ...'
		, columns:[[
				{field:'to', title:'提醒对象', width:100, align:'center'}
				, {field:'from', title:'发送邮箱', width:150, align:'center', formatter: function(value, rowData){
					return "<a href='javascript:void(0)' onclick='ShowFrom(\"" + value + "\")'>" + value + "</a>";
				}}
				, {field:'subject', title:'标题', width:150, align:'center', formatter: function(value, rowData){
					return "<a href='javascript:void(0)' onclick='ShowBody(\"" + rowData.rawto + "\")'>" + value + "</a>";
				}}
				, {field:'run', title:'执行状态', width:60, align:'center', formatter: function(value, rowData){
					if(0 != value)
						return "<font color=green>正在执行</font>";
					else 
						return "<font color=red>停止执行</font>";
				}}
				, {field:'day', title:'提前几天提醒', width:80, align:'center'}
				, {field:'cycle', title:'提交周期(天)', width:80, align:'center'}
				, {field:'sucess', title:'成功数', width:60, align:'center'}
				, {field:'failure', title:'失败数', width:60, align:'center', formatter: function(value, rowData){
					if(0 == value)
						return value;
					return "<font color=red>" + value + "</font>";
				}}
				, {field:'firstdt', title:'第一次执行时间', width:100, align:'center'}
				, {field:'lastdt', title:'最后一次执行时间', width:100, align:'center'}
				, {field:'#opt', title:'操作', width:100, align:'center', formatter: function(value, rowData){
					var str = "<a href='javascript:void(0)' onclick='DeleteEmail(\"" + rowData.rawto + "\")'>删除</a>&nbsp;";
					if(0 != rowData.run)
						str += "<font color=gray>执行</font>";
					else
						str += "<a href='javascript:void(0)' onclick='StartEmail(\"" + rowData.rawto + "\")'>执行</a>";
					str += "&nbsp;";
					if(0 != rowData.run)
						str += "<a href='javascript:void(0)' onclick='StopEmail(\"" + rowData.rawto+ "\")'>停止</a>";
					else
						str += "<font color=gray>停止</font>";
					return str;					
				}}
			]]
  	});
});

/*显示收件人信息*/
function ShowFrom(email){
}

/*显示邮件内容*/
function ShowBody(id){
}

/*删除操作*/
function DeleteEmail(to){
	top.$.messager.confirm('确认操作', '确实要删除到期邮件提醒吗？'
		, function(b){
			if(!b) return;
			$.ajax({type:'POST'
				, url: 'sendemail-last-action.php'
				, data: 'type=Delete&to=' + to
				, dataType: 'json'
				, error: function(XMLHttpRequest, textStatus, errorThrown){
					top.$.messager.alert('错误', '请求失败:' + errorThrown, 'error');
				}
				,success: function(msg){
					if(msg.success){
						top.$.messager.alert('操作成功', msg.msg, 'info');
						$('#sendemail_last_tt').datagrid('load');
					} else {
						top.$.messager.alert('操作失败', msg.msg, 'error');
					}
				}
			});
		}
	);
}

/*启动操作*/
function StartEmail(to){
	$.ajax({type:'POST'
		, url: 'sendemail-last-action.php'
		, data: 'type=Start&to=' + to
		, dataType: 'json'
		, error: function(XMLHttpRequest, textStatus, errorThrown){
			top.$.messager.alert('错误', '请求失败:' + errorThrown, 'error');
		}
		,success: function(msg){
			if(msg.success){
				$('#sendemail_last_tt').datagrid('reload');
			} else {
				top.$.messager.alert('操作失败', msg.msg, 'error');
			}
		}
	});
}

/*停止操作*/
function StopEmail(to){
	$.ajax({type:'POST'
		, url: 'sendemail-last-action.php'
		, data: 'type=Stop&to=' + to
		, dataType: 'json'
		, error: function(XMLHttpRequest, textStatus, errorThrown){
			top.$.messager.alert('错误', '请求失败:' + errorThrown, 'error');
		}
		,success: function(msg){
			if(msg.success){
				$('#sendemail_last_tt').datagrid('reload');
			} else {
				top.$.messager.alert('操作失败', msg.msg, 'error');
			}
		}
	});
}